package com.zsy.service.impl;

import com.zsy.entity.*;
import com.zsy.mapper.ISalesMapper;
import com.zsy.service.ISalesService;
import com.zsy.utils.Result;
//import com.zsy.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesServiceImpl implements ISalesService {
    @Autowired
    private ISalesMapper ism;

    // 新增客户
    @Override
    public Result addClient(MyClient mc) {
//        Map<String, Object> map = ThreadLocalUtil.get();
//        String dp = (String) map.get("department");
//        if (dp.equals("销售部")) {
            if (mc.getClientName() == null)
                return Result.error("客户名称不能为空");
            if (mc.getPhone() == null)
                return Result.error("联系方式不能为空");
            if (mc.getClientManager() == null)
                return Result.error("客户经理不能为空");
            int i = ism.insertClient(mc);
            if (i > 0) {
                return Result.ok("客户添加成功");
            } else {
                return Result.error("客户添加失败");
            }
//        } else {
//            return Result.error("权限不足");
//        }
    }

    // 查询客户
    @Override
    public Result queryClient() {
        List<MyClient> myClients = ism.queryClient();
        for (MyClient myClient : myClients) {
            System.out.println(myClient.toString());
        }
        if(myClients != null){
            return Result.ok(myClients);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 删除客户
    @Override
    public Result deleteClient(int clientId) {
        int i = -1;
        i = ism.deleteClient(clientId);
        if (i > 0) {
            return Result.ok("客户删除成功");
        } else {
            return Result.error("客户删除失败");
        }
    }

    // 新增采购需求
    @Override
    public Result addPurchaseRequirements(PurchaseRequirements pr) {
        int i = -1;
        i = ism.insertPurchaseRequirements(pr);
        if (i > 0) {
            return Result.ok("采购需求添加成功");
        } else {
            return Result.error("采购需求添加失败");
        }
    }

    // 查询合同信息
    @Override
    public Result selectContractAll() {
        List<ContractUser> contractUsers = ism.selectContractAll();
        for (ContractUser contractUser : contractUsers) {
            System.out.println(contractUser.toString());
        }
        if(contractUsers != null){
            return Result.ok(contractUsers);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 联系情况
    @Override
    public Result getContactInformation() {
        List<ContactInformation> contactInformation = ism.selectContactInformation();
        for (ContactInformation ci : contactInformation) {
            System.out.println(ci.toString());
        }
        if(contactInformation != null){
            return Result.ok(contactInformation);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 重点客户跟进
    @Override
    public Result getKeyCustomers() {
        List<KeyCustomers> keyCustomers = ism.selectKeyCustomers();
//        Map<String, Object> mp = ThreadLocalUtil.get();
//        String department = (String) mp.get("department");
//        if(department.equals("销售部") || department.equals("管理员")){
            for (KeyCustomers kc : keyCustomers) {
                System.out.println(kc.toString());
            }
            if(keyCustomers != null){
                return Result.ok(keyCustomers);
            } else {
                return Result.error("未获取到数据");
            }
//        }
//        else {
//            return Result.error("没有权限");
//        }

    }

    // 修改重点客户跟进
    @Override
    public Result updateKeyCustomers(KeyCustomers keyCustomers) {
        int i = -1;
        i = ism.updateKeyCustomers(keyCustomers);
        if (i > 0) {
            return Result.ok("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    // 查询商机
    @Override
    public Result getAllBusiness() {
        List<BusinessManagement> businessManagements = ism.selectBusinessManagement();
        for (BusinessManagement bm : businessManagements) {
            System.out.println(bm.toString());
        }
        if(businessManagements != null) {
            return Result.ok(businessManagements);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 排行榜
    @Override
    public Result rankingList() {
        List<RankingList> rankingList = ism.selectRankingList();
        for (RankingList rl : rankingList) {
            System.out.println(rl.toString());
        }
        if(rankingList != null){
            return Result.ok(rankingList);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 查询客户总数
    @Override
    public Result getAllClientQuality() {
        int allClientQuality = ism.getAllClientQuality();
        if(allClientQuality > -1){
            return Result.ok(allClientQuality);
        } else {
            return Result.error("未获取到数据");
        }
    }

    // 查询商机总数
    @Override
    public Result getAllBusinessQuantity() {
        int allBusinessQuantity = ism.getAllBusinessQuantity();
        if (allBusinessQuantity > -1) {
            return Result.ok(allBusinessQuantity);
        } else {
            return Result.error("未获取到数据");
        }
    }
}
